#include <platform.h>
#include "HelloWorldScene.h"
#include "file_mgr.h"

using namespace cocos2d;

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::node();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::node();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());

        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////

        // 1. Add a menu item with "X" image, which is clicked to quit the program.

        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);

        // 2. Add a label shows "Hello World".

        // Create a label and initialize with string "Hello World".
        CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Thonburi", 64);
        CC_BREAK_IF(! pLabel);

        // Get window size and place the label upper. 
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        pLabel->setPosition(ccp(size.width / 2, size.height - 20));

        // Add the label to HelloWorld layer as a child layer.
        this->addChild(pLabel, 1,101);

		// 3. Add add a splash screen, show the cocos2d splash image.
		//CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png");
		//CC_BREAK_IF(! pSprite);

		// Place the sprite on the center of the screen
		//pSprite->setPosition(ccp(size.width/2, size.height/2));

		// Add the sprite to HelloWorld layer as a child layer.
		//this->addChild(pSprite, 0);

		FileMbx MbxMgr;
		MbxMgr.LoadMbxFile("095_3.mbx");

		PianoKeyLayer = PianoKey::node();
		PianoKeyLayer->setPosition(ccp(100,130));
		addChild(PianoKeyLayer,2);
		schedule(schedule_selector(HelloWorld::ScheduleProcess),0);

		
		bRet = true;
	} while (0);

	return bRet;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
	// "close" menu item clicked
	CCDirector::sharedDirector()->end();
}


void HelloWorld::ScheduleProcess(ccTime dt)
{

	//static float lastdt = 0;
	////CCLabelTTF *pLabel = (CCLabelTTF*)this->getChildByTag(101);
	//char buf[32];
	//sprintf(buf,"Dtime:%0.8f",dt);
	//CCLog(buf);
	////pLabel->setString(buf);
	//lastdt = dt;

	/*
	CCDirector *pDirector = CCDirector::sharedDirector();
	char buf[32];
	sprintf(buf,"Dtime:%0.8f",pDirector->getAnimationInterval());
	CCLog(buf);
	*/

	//      struct cc_timeval currentTime;
	//CCTime::gettimeofdayCocos2d(&currentTime, NULL);
	//double duration = currentTime.tv_sec * 1000.0 + currentTime.tv_usec / 1000.0;
	//CCLog("%d,%d,%f",currentTime.tv_sec,currentTime.tv_usec,duration);

	//CCTime::timersubCocos2d(&currentTime, pTimer->getStartTime(), &currentTime);
	//double duration = currentTime.tv_sec * 1000.0 + currentTime.tv_usec / 1000.0;

	// return in milliseconds
	//pTimer->setAverageTime((pTimer->getAverageTime() + duration) / 2.0f);


 

	static float tmp = 0.0f;
	tmp += 0.016f;
	if(tmp > 3.0f)
		tmp = 0.0f;
	PianoKeyLayer->DrawRate(1,tmp);
	//CCLog("dt:%d",dt);
}